package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.AccountBean;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AliPayActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private String accountId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ali_pay);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("支付宝账户");
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                confirm();
                break;
        }
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.GetAccount,AliPayActivity.this)
                .params("accounttype","2")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        AccountBean parentBean= DealResult.getInstance().dealDatas(AliPayActivity.this,response,new TypeToken<AccountBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            setDatas(parentBean.getBody());
                        }else {
                            ToastUtils.toastForShort(AliPayActivity.this,parentBean.getResult());

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(AliPayActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(AccountBean.BodyBean body) {
        etNum.setText(body.getValue1());
        etName.setText(body.getValue2());
        accountId=""+body.getAcountid();
    }

    private void confirm() {
        String num=etNum.getText().toString();
        String name=etName.getText().toString();

        if(TextUtils.isEmpty(num)){
            ToastUtils.toastForShort(this,"账号不能为空");return;
        }
        if(TextUtils.isEmpty(name)){
            ToastUtils.toastForShort(this,"姓名不能为空");return;
        }

        loadingDialog.show();
        OkgoUtil.post(Http.EditAccount,AliPayActivity.this)
                .params("accounttype","2")
                .params("value1",num)
                .params("value2",name)
                .params("acountid",accountId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean= DealResult.getInstance().dealDatas(AliPayActivity.this,response,new TypeToken<BaseBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            SPUtils.put(AliPayActivity.this,Constant.IS_ALIPAY,1);
                            AliPayActivity.this.finish();
                        }else {
                        }
                        ToastUtils.toastForShort(AliPayActivity.this,parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(AliPayActivity.this,getString(R.string.bad_net));
                    }
                });
    }
}
